package com.yun.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yun.mapper.CategoryMapper;
import com.yun.pojo.Category;
import com.yun.unit.Code;
import com.yun.unit.PageUnit;
import com.yun.unit.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService{
    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    public R selByUserId(PageUnit pageUnit) {
        PageHelper.startPage(pageUnit.getPagenum(),pageUnit.getPagesize());
        //向数据库查询
        List<Category> categories = categoryMapper.selByUserId(pageUnit.getId());
        if (categories != null){
            PageInfo<Category> categoryPageInfo = new PageInfo<>(categories);
            return new R(Code.get_ok,"查询成功",categoryPageInfo);
        }
        return new R(Code.get_err,"查询失败");
    }

    @Override
    public R delById(Integer id) {
        int i = categoryMapper.updById(id);
        if (i>0){
            return new R(Code.delete_ok,"删除成功");
        }
        return new R(Code.delete_err,"删除失败");
    }

    @Override
    public R detail(Integer id) {
        Category category = categoryMapper.detail(id);
        if (category != null){
            return new R(Code.get_ok,"查看成功",category);
        }
        return new R(Code.get_err,"查看失败,请稍后再试");
    }
    //修改分类
    @Override
    public R upd(Category category) {
        int upd = categoryMapper.upd(category);
        if (upd>0){
            return new R(Code.update_ok,"修改成功");
        }
        return new R(Code.update_err,"修改失败");
    }

    //新增分类
    @Override
    public R ins(Category category) {
        int ins = categoryMapper.ins(category);
        if (ins>0){
            return new R(Code.save_ok,"新增成功");
        }
        return new R(Code.save_err,"新增失败");
    }
    //查询分类根据名称
    @Override
    public R selByName(PageUnit pageUnit) {
        PageHelper.startPage(pageUnit.getPagenum(),pageUnit.getPagesize());
        List<Category> categories = categoryMapper.selByName(pageUnit.getKeywords(), pageUnit.getId());
        if (categories != null){
            PageInfo<Category> categoryPageInfo = new PageInfo<>(categories);
            return new R(Code.get_ok,"查询成功",categoryPageInfo);
        }
        return new R(Code.get_err,"查询失败");
    }

    //查看所有被假删的分类
    @Override
    public R selByDel(PageUnit pageUnit) {
        PageHelper.startPage(pageUnit.getPagenum(),pageUnit.getPagesize());
        List<Category> categories = categoryMapper.selByDel(pageUnit.getId());
        if (categories != null){
            PageInfo<Category> categoryPageInfo = new PageInfo<>(categories);
            return new R(Code.get_ok,"查询成功",categoryPageInfo);
        }
        return new R(Code.get_err,"查询失败");
    }

    //还原或者删除分类
    @Override
    public R updByDel(Integer id, Integer del) {
        int i = categoryMapper.updByIdD(id, del);
        if (i>0){
            return new R(Code.update_ok,"删除成功");
        }
        return new R(Code.update_err,"删除失败");
    }

    @Override
    public R selAll(Integer id) {
        List<Category> categories = categoryMapper.selByUserIdD(id);
        if (categories != null){
            return new R(Code.get_ok,"查询成功",categories);
        }
        return new R(Code.get_err,"查询失败");
    }
}
